import { BasicFetchResult } from '../../model/baseModel';
import { defHttp } from '@/utils/http/axios';
import { downloadBlob } from '@/utils/file/download';
const apiKey = 'reportDataSource';
export const page = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: `/${apiKey}/page`, params });
export const list = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: `/${apiKey}/list`, params });
export const add = (params?: any) => defHttp.post({ url: `/${apiKey}/add`, params });
export const edit = (params?: any) => defHttp.post({ url: `/${apiKey}/edit`, params });
export const del = (ids: string[]) => defHttp.post({ url: `/${apiKey}/delete`, params: ids });

export const exp = (params?: any) => {
  return new Promise<void>((resolve, reject) => {
    defHttp
      .get(
        { responseType: 'blob', url: `/${apiKey}/export`, params },
        { isReturnNativeResponse: true },
      )
      .then((res) => {
        downloadBlob(res);
        resolve();
      })
      .catch(() => {
        reject();
      });
  });
};
/**
 * 通用请求
 * @param url
 * @param method
 * @param params
 * @returns
 */
export const common = (url: string, method: string, params?: any) => {
  if (method.toLowerCase() === 'get') {
    return defHttp.get<BasicFetchResult<any>>({ url, params });
  } else if (method.toLowerCase() === 'post') {
    return defHttp.post<BasicFetchResult<any>>({ url, params });
  } else if (method.toLowerCase() === 'put') {
    return defHttp.post<BasicFetchResult<any>>({ url, params });
  } else if (method.toLowerCase() === 'delete') {
    return defHttp.post<BasicFetchResult<any>>({ url, params });
  } else if (method.toLowerCase() === 'download') {
    new Promise<void>((resolve, reject) => {
      defHttp
        .get({ responseType: 'blob', url, params }, { isReturnNativeResponse: true })
        .then((res) => {
          downloadBlob(res);
          resolve();
        })
        .catch(() => {
          reject();
        });
    });
  }
};
